package com.yun.studyprovider.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class SendMessageController {

    private final RabbitTemplate rabbitTemplate;

    public SendMessageController(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @GetMapping("/sendMessage")
    public String sendMessage() {
        String msgId = UUID.randomUUID().toString();
        String msgData = "study direct msg hello world!";
        String msgTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map = new HashMap<>();
        map.put("msgId",msgId);
        map.put("msgData",msgData);
        map.put("msgTime",msgTime);
        //将消息携带键值：StudyDirectRouting 发送到交换机StudyDirectExchange中
        rabbitTemplate.convertAndSend("StudyDirectExchange","StudyDirectRouting",map);
        return "Direct 信息推送成功！";
    }

    @GetMapping("/sendTopicMessage1")
    public String sendTopicMessage1(){
        String msgId = UUID.randomUUID().toString();
        String msgData = "study topic msge hello man";
        String msgTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> manMap = new HashMap<>();
        manMap.put("msgId",msgId);
        manMap.put("msgData",msgData);
        manMap.put("msgTime",msgTime);
        rabbitTemplate.convertAndSend("topicExchange","topic.man",manMap);
        return "Topic man 信息推送成功！";
    }

    @GetMapping("/sendTopicMessage2")
    public String sendTopicMessage2(){
        String msgId = UUID.randomUUID().toString();
        String msgData = "study topic msge hello woman";
        String msgTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> womanMap = new HashMap<>();
        womanMap.put("msgId",msgId);
        womanMap.put("msgData",msgData);
        womanMap.put("msgTime",msgTime);
        rabbitTemplate.convertAndSend("topicExchange","topic.woman",womanMap);
        return "Topic woman 信息推送成功！";
    }

    @GetMapping("/sendFanoutMessage")
    public String sendFanoutMessage(){
        String msgId = UUID.randomUUID().toString();
        String msgData = "study Fanout msge hello";
        String msgTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> fanoutMap = new HashMap<>();
        fanoutMap.put("msgId",msgId);
        fanoutMap.put("msgData",msgData);
        fanoutMap.put("msgTime",msgTime);
        rabbitTemplate.convertAndSend("fanoutExchange",null,fanoutMap);
        return "fanout 信息推送成功！";
    }

    @GetMapping("/TestMessageAck")
    public String TestMessageAck() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: non-existent-exchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", map);
        return "ok";
    }

    @GetMapping("/TestMessageAck2")
    public String TestMessageAck2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: non-existent-exchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("lonelyDirectExchange", "StudyDirectQueue", map);
        return "ok";
    }
}
